using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace App
{
    internal class CGroup : GroupBox
    {
        public event CPopupEventHandler OnPopup;

        private bool hoverplus, pressed;

        public CGroup()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.Margin = new Padding(1);
            this.Size = new Size(79, 79);
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            if (OnPopup != null && this.hoverplus)
                OnPopup(this);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            this.pressed = true;
            this.Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            this.pressed = false;
            this.Invalidate();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (new Rectangle(this.Width - 20, this.Height - 20, 18, 18).Contains(e.X, e.Y))
                this.hoverplus = true;
            else
                this.hoverplus = false;

            this.Invalidate();
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            this.hoverplus = false;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

            switch (Common.Theme)
            {
                case Common.ColorScheme.Blue:
                    {
                        Rectangle rect1 = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
                        Rectangle rect2 = new Rectangle(0, this.Height - 18, this.Width - 2, 16);
                        Rectangle rect3 = new Rectangle(this.Width - 18, this.Height - 18, 15, 15);

                        DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(Color.FromArgb(237, 242, 248)), rect1, 3f);

                        GraphicsPath gp = new GraphicsPath();
                        gp.AddArc(rect2.X, rect2.Y - 4f, 6f, 6f, 180, 90);
                        gp.AddArc(rect2.X + rect2.Width - 6f, rect2.Y - 5f, 6f, 6f, 270, 90);
                        gp.AddArc(rect2.X + rect2.Width - 6f, rect2.Y + rect2.Height - 6f, 6f, 6f, 0, 90);
                        gp.AddArc(rect2.X, rect2.Y + rect2.Height - 6f, 6f, 6f, 90, 90);
                        gp.CloseFigure();
                        e.Graphics.SetClip(gp, CombineMode.Intersect);
                        gp.Dispose();

                        using (Brush b = new SolidBrush(Color.FromArgb(193, 217, 241)))
                        {
                            e.Graphics.FillRectangle(b, rect2);
                        }
                        
                        e.Graphics.Clip = new Region();

                        StringFormat sf = new StringFormat();
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;
                        using (Brush b = new SolidBrush(Color.FromArgb(21, 66, 139)))
                        {
                            e.Graphics.DrawString(this.Text, this.Font, b, rect2, sf);
                        }

                        if (OnPopup != null)
                        {
                            if (this.hoverplus)
                                DrawHelper.RenderSelection(e.Graphics, rect3, 2f, this.pressed);
                            using (Brush b = new SolidBrush(Color.FromArgb(21, 66, 139)))
                            {
                                e.Graphics.DrawString("+", this.Font, b, rect3, sf);
                            }
                            
                        }

                        rect1.Offset(-1, -1);
                        DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(Color.FromArgb(197, 210, 223)), rect1, 3f);
                    }
                    break;
                case Common.ColorScheme.Custom:
                    {
                        Rectangle rect1 = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
                        Rectangle rect2 = new Rectangle(0, this.Height - 18, this.Width - 2, 16);
                        Rectangle rect3 = new Rectangle(this.Width - 18, this.Height - 18, 15, 15);

                        DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(DrawHelper.GetColor(1.05, Common.Color)), rect1, 3f);

                        GraphicsPath gp = new GraphicsPath();
                        gp.AddArc(rect2.X, rect2.Y - 4f, 6f, 6f, 180, 90);
                        gp.AddArc(rect2.X + rect2.Width - 6f, rect2.Y - 5f, 6f, 6f, 270, 90);
                        gp.AddArc(rect2.X + rect2.Width - 6f, rect2.Y + rect2.Height - 6f, 6f, 6f, 0, 90);
                        gp.AddArc(rect2.X, rect2.Y + rect2.Height - 6f, 6f, 6f, 90, 90);
                        gp.CloseFigure();
                        e.Graphics.SetClip(gp, CombineMode.Intersect);
                        gp.Dispose();
                        e.Graphics.FillRectangle(new SolidBrush(DrawHelper.GetColor(0.975, Common.Color)), rect2);
                        e.Graphics.Clip = new Region();

                        StringFormat sf = new StringFormat();
                        sf.Alignment = StringAlignment.Center;
                        sf.LineAlignment = StringAlignment.Center;

                        e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(SystemColors.ControlText), rect2, sf);

                        if (OnPopup != null)
                        {
                            if (this.hoverplus)
                                DrawHelper.RenderSelection(e.Graphics, rect3, 2f, this.pressed);

                            e.Graphics.DrawString("+", this.Font, new SolidBrush(this.ForeColor), rect3, sf);
                        }

                        rect1.Offset(-1, -1);
                        DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(DrawHelper.GetColor(0.85, Common.Color)), rect1, 3f);
                    }
                    break;
            }
        }
    }
}
